<?php

/**
 * @author Tuan Anh
 * @copyright 2012
 */

class MapManager {
    private $db;
    
    function __construct($db){
        $this->db = $db;
    }
    /**
     * success  : return uid
     * fail     : return "-1"
     * */
    public function getdata(){
        $query = "SELECT * FROM record";
        $this->db->dbQuery($query);
        
        return $this->db->result['content'];
    }

    public function radius($lat, $lng, $d){
        $r = $d/6371;

        $latmin = $lat - $r;
        $latmax = $lat + $r;

        //$latT = asin(sin($lat)/cos(r));
        $dentalng = asin(sin($r)/cos($lat));

        $lngmin = $lng - $dentalng;
        $lngmax = $lng + $dentalng;
        $query = "SELECT lat, lng, latrad, lngrad, title, address, district, description, rid, price FROM record WHERE  (latrad >= $latmin AND latrad <= $latmax) AND (lngrad >= $lngmin AND lngrad <= $lngmax)
                    HAVING acos(sin($lat) * sin(latrad) + cos($lat) * cos(latrad) * cos(lngrad - ($lng))) <= $r;
                 ";

        $this->db->dbQuery($query);
        $result = $latmin."-".$latmax."-".$lngmin."-".$lngmax;
//        return $query."<br>".$this->db->fetchToJSON();
        return $this->db->fetchToJSON();
    }

}

?>